package com.shyt.api;

import com.shyt.dao.SysRoleVo;
import com.shyt.dao.SysUserVo;
import com.shyt.filter.annotation.RecordLogsAspectAt;
import com.shyt.impl.SysRoleServiceImpl;
import com.shyt.util.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@Slf4j
@RequestMapping("api/")
@CrossOrigin
public class SysRoleApi {
    @Resource
    SysRoleServiceImpl sysRoleServiceImpl;

    @RecordLogsAspectAt
    @PostMapping("getAllRoles")
    public ApiResponse getAllRoles() throws Exception {
        List<SysRoleVo> sysUserVos = sysRoleServiceImpl.getAllRoles();
        //return new ApiResponse("200", "调用成功", sysUserVos);
        return ApiResponse.ok(sysUserVos);
    }
    @RecordLogsAspectAt
    @PostMapping("deleteRole")
    public ApiResponse deleteRole(@RequestBody SysRoleVo sysRoleVo) throws Exception {
        sysRoleServiceImpl.deleteRole(sysRoleVo);
        //return new ApiResponse("200", "调用成功", sysUserVos);
        return ApiResponse.ok("");
    }

    @RecordLogsAspectAt
    @PostMapping("getRoleByRoleName")
    public ApiResponse getRoleByRoleName(@RequestBody SysRoleVo sysRoleVo) throws Exception {
        List<SysRoleVo> sysUserVos = sysRoleServiceImpl.getRoleByRoleName(sysRoleVo);
        //return new ApiResponse("200", "调用成功", sysUserVos);
        return ApiResponse.ok(sysUserVos);
    }

    @RecordLogsAspectAt
    @PostMapping("selectRoleDeatil")
    public ApiResponse selectRoleDeatil(@RequestBody SysRoleVo sysRoleVo) throws Exception {
        SysRoleVo sysUserVo = sysRoleServiceImpl.selectRoleDeatil(sysRoleVo);
        //return new ApiResponse("200", "调用成功", sysUserVos);
        return ApiResponse.ok(sysUserVo);
    }

    @RecordLogsAspectAt
    @PostMapping("editRole")
    public ApiResponse editRole(@RequestBody SysRoleVo sysRoleVo) throws Exception {
        sysRoleServiceImpl.editRole(sysRoleVo);
        //return new ApiResponse("200", "调用成功", sysUserVos);
        return ApiResponse.ok("");
    }

    @RecordLogsAspectAt
    @PostMapping("addRole")
    public ApiResponse addRole(@RequestBody SysRoleVo sysRoleVo) throws Exception {
        Authentication authentication =
                SecurityContextHolder.getContext().getAuthentication();
        if (null == authentication) {
            throw new Exception("用户信息为空，用户信息有误，请联系管理员");
        }
        SysUserVo sysUserVo = (SysUserVo) authentication.getPrincipal();
        if (null == sysUserVo) {
            return null;
        }


        sysRoleVo.setId(String.valueOf(getLong(new Random(), 100000l)));
        sysRoleVo.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:MM:ss").format(new Date()));
        sysRoleVo.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:MM:ss").format(new Date()));
        sysRoleVo.setCreateUser(sysUserVo.getUsername());
        sysRoleServiceImpl.addRole(sysRoleVo);
        //return new ApiResponse("200", "调用成功", "");
        return ApiResponse.ok("");
    }


    public long getLong(Random random, long n) {
        long bits, val;
        do {
            bits = (random.nextLong() << 1) >>> 1;
            val = bits % n;
        } while (bits - val + (n - 1) < 0l);
        return val;
    }

}
